@echo off
setlocal enabledelayedexpansion

set "project_name=xhs-login-service"
set "main_class=com.xhs.XhsLoginServiceApplication"
set "jre_name=jdk-17.0.15+6-jre"
set "playwright_mirror=https://npmmirror.com/mirrors/playwright"

set "script_dir=%~dp0"
set "project_dir=%script_dir%"
set "target_dir=%project_dir%target"
set "xhs_dir=%project_dir%xhs"
set "jre_source_dir=%project_dir%jre\%jre_name%"

echo =======================================================
echo     %project_name% - Universal Build and Run Script
echo =======================================================
echo.

echo [STEP 1/5] Checking for Maven environment...
call mvn -v >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Maven is not found on your system PATH.
    echo Please install Apache Maven and ensure it's accessible from the command line.
    pause
    exit /b 1
)
echo [INFO] Maven found.
echo.

echo [STEP 2/5] Compiling project and gathering dependencies...
call mvn clean compile dependency:copy-dependencies -DskipTests
if errorlevel 1 (
    echo [ERROR] Maven build failed. Please check the logs above.
    pause
    exit /b 1
)
echo [INFO] Maven build completed successfully.

echo [STEP 2a/5] Running ProGuard obfuscation on business code...
call mvn process-classes -Pobfuscate -DskipTests
if errorlevel 1 (
    echo [WARNING] ProGuard obfuscation failed, using original classes
) else (
    if exist "%target_dir%\classes-obfuscated" (
        echo [INFO] ProGuard obfuscation successful, replacing classes with obfuscated version
        rmdir /s /q "%target_dir%\classes"
        move "%target_dir%\classes-obfuscated" "%target_dir%\classes"
        echo [SUCCESS] Business code has been obfuscated successfully.
    ) else (
        echo [WARNING] Obfuscated classes not found, using original classes
    )
)
echo.

echo [STEP 3/5] Preparing runtime environment...
if exist "%xhs_dir%" (
    echo [INFO] Cleaning up previous runtime directory...
    rmdir /s /q "%xhs_dir%"
)
mkdir "%xhs_dir%"
mkdir "%xhs_dir%\license"
mkdir "%xhs_dir%\data"
mkdir "%xhs_dir%\data\.ms-playwright"

set "playwright_data_dir=%xhs_dir%\data"
set "PLAYWRIGHT_BROWSERS_PATH=%playwright_data_dir%\.ms-playwright"
set "PLAYWRIGHT_DOWNLOAD_HOST=%playwright_mirror%"

echo [INFO] Pre-installing Playwright browsers...
echo [INFO] Browsers will be downloaded to: %PLAYWRIGHT_BROWSERS_PATH%
echo [INFO] Using mirror for faster download: %PLAYWRIGHT_DOWNLOAD_HOST%

call mvn exec:java -Dexec.mainClass="com.microsoft.playwright.CLI" -Dexec.args="install --with-deps" -DPLAYWRIGHT_BROWSERS_PATH="%PLAYWRIGHT_BROWSERS_PATH%" -DPLAYWRIGHT_DOWNLOAD_HOST="%PLAYWRIGHT_DOWNLOAD_HOST%"
echo.
if not exist "%PLAYWRIGHT_BROWSERS_PATH%\chromium-*" (
    echo [WARNING] Playwright browser pre-installation may have failed or is incomplete.
) else (
    echo [SUCCESS] Playwright browsers have been pre-installed successfully!
)
echo.

echo [STEP 4/5] Setting up application files...

echo [INFO] Copying JRE...
xcopy "%jre_source_dir%" "%xhs_dir%\%jre_name%\" /e /i /h /q
if not exist "%xhs_dir%\%jre_name%\bin\java.exe" (
    echo [ERROR] JRE not found at source: %jre_source_dir%
    pause
    exit /b 1
)

echo [INFO] Copying application classes and resources...
mkdir "%xhs_dir%\app"
xcopy "%target_dir%\classes" "%xhs_dir%\app" /e /i /h /q

echo [INFO] Copying dependencies...
mkdir "%xhs_dir%\lib"
xcopy "%target_dir%\dependency" "%xhs_dir%\lib" /e /i /h /q


echo.

echo [STEP 5/5] Creating final start script and launching...
cd /d "%xhs_dir%"

REM Build classpath
set "CLASSPATH_STR=.\app"
for %%f in (.\lib\*.jar) do set "CLASSPATH_STR=!CLASSPATH_STR!;%%f"

REM Copy template and replace placeholders
copy "%project_dir%start_template.bat" "start.bat" >nul

REM Replace placeholders using PowerShell (more reliable than batch string replacement)
powershell -Command "(Get-Content 'start.bat') -replace 'PROJECT_NAME', '%project_name%' -replace 'JRE_NAME', '%jre_name%' -replace 'MAIN_CLASS', '%main_class%' -replace 'CLASSPATH_PLACEHOLDER', '!CLASSPATH_STR!' | Set-Content 'start.bat'"

echo [SUCCESS] Setup complete! A reusable 'start.bat' has been created in the 'xhs' directory.
echo You can now share the entire 'xhs' folder, and others can directly use 'start.bat'.
echo.
echo Launching the application now...
echo -------------------------------------------------------
call start.bat

exit /b 0 